﻿using System.Collections.Generic;
using System.Diagnostics;

namespace MakeBorderless
{
    internal struct GroupItem
    {
        public GroupItem(string name, bool isStyled)
        {
            Name = name;
            IsStyled = isStyled;
        }

        public string Name;
        public bool IsStyled;
    }

    internal class ProcessGroup
    {
        List<Process> _processes;
        List<GroupItem> _groupItems;
        string _similarName;
        bool _useWindowName;

        public ProcessGroup(string name, bool useWinName)
        {
            _processes = new List<Process>();
            _groupItems = new List<GroupItem>();
            _similarName = name;
            _useWindowName = useWinName;
        }

        public string Name
        {
            get
            {
                return _similarName;
            }
        }

        public bool UsingWindowName
        {
            get
            {
                return _useWindowName;
            }
        }

        public bool AllStyled
        {
            get
            {
                foreach (Process p in _processes)
                {
                    if (!Styler.IsStyled(p))
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public int Count
        {
            get
            {
                return _processes.Count;
            }
        }

        public List<GroupItem> GroupItems
        {
            get
            {
                return _groupItems;
            }
        }

        public List<Process> Processes
        {
            get
            {
                return _processes;
            }
        }

        public bool Add(Process proc)
        {
            foreach (Process p in _processes)
            {
                if (!(p.MainWindowHandle != proc.MainWindowHandle &&
                        GetName(p) == GetName(proc)))
                {
                    return false;
                }
            }
            _processes.Add(proc);
            GroupItem temp = new GroupItem(GetName(proc), Styler.IsStyled(proc));
            _groupItems.Add(temp);
            return true;
        }

        public void Refresh()
        {
            List<Process> pCopy = new List<Process>(_processes);
            List<Process> newProcs;

            if (_useWindowName)
            {
                newProcs = GetProcessesByWindow(_similarName);
            }
            else
            {
                newProcs = new List<Process>(Process.GetProcessesByName(_similarName));
            }
            foreach (Process p in _processes)
            {
                if (!p.HasExited)
                {
                    newProcs.Add(p);
                }
            }
            _processes = newProcs;
            UpdateGroupItems();
        }

        private void UpdateGroupItems()
        {
            List<GroupItem> items = new List<GroupItem>();
            foreach (Process p in _processes)
            {
                GroupItem i = new GroupItem(GetName(p), Styler.IsStyled(p));
                items.Add(i);
            }
            _groupItems = items;
        }

        private string GetName(Process proc)
        {
            if (proc == null)
            {
                return "NULL PROCESS";
            }
            return (_useWindowName ? proc.MainWindowTitle : proc.ProcessName);
        }

        private List<Process> GetProcessesByWindow(string name)
        {
            Process[] procs = Process.GetProcesses();
            List<Process> outprocs = new List<Process>();
            foreach (Process p in procs)
            {
                if (p.MainWindowTitle.ToLower() == name.ToLower())
                {
                    outprocs.Add(p);
                }
            }
            return outprocs;
        }
    }
}